package com.jiazhong.example.project.bookstore.backend.controller;

import com.jiazhong.example.project.bookstore.backend.service.BookService;
import com.jiazhong.example.project.bookstore.commons.bean.Book;
import com.jiazhong.example.project.bookstore.commons.util.JsonResult;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/book")
public class BookController {
    @Resource
    private BookService bookService;

    @GetMapping("/{page}")
    public JsonResult find(@PathVariable("page") int page) {
        return bookService.find(page);
    }

    @GetMapping
    public JsonResult find() {
        return bookService.find(1);
    }

    @PostMapping
    public JsonResult save(Book book, @RequestParam("file") MultipartFile file) throws IOException {
        return bookService.saveBook(book, file);
    }

    @GetMapping("/category/{id}")
    public JsonResult findBookByCategoryId(@PathVariable("id") String categoryId) {
        return bookService.findBookByCategoryId(categoryId);
    }

    @GetMapping("/find_all")
    public JsonResult findAll() {
        return bookService.findAll();
    }
}
